<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class WayrestController extends Uuch_RestController {
    public function indexAction() {
    }
    
    public function getAction() {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
        $input = file_get_contents("php://input");
        $params = Zend_Json::decode($input);
        $ifGoOn = $this->analysisParams($params);
        if($ifGoOn["Result"] != 1) {
            $data = Zend_Json::encode ( $ifGoOn );
            echo $data;
            return;
        }
        if(!method_exists($this,$params["functionName"])){
            return array("Result"=>1006,"ResultMessage"=>"所需要的服务不存在或者暂不可用");
        }
        $functionName = $params["functionName"];
        $returnData = $this->$functionName($params);
        if(!$returnData) {
            $returnData = array("Result"=>1007,"ResultMessage"=>"Mobile服务调用没有返回任何报文");
        }
        $data = Zend_Json::encode ( $returnData );
        echo $data;
        return;
    }
    
    public function postAction() {
        return false;
    }
    
    public function putAction() {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
        $input = file_get_contents("php://input");
        $params = Zend_Json::decode($input);
        $ifGoOn = $this->analysisParams($params);
        if($ifGoOn["Result"] != 1) {
            $data = Zend_Json::encode ( $ifGoOn );
            echo $data;
            exit();
        }
        $functionName = $params["functionName"];
        $returnData = $this->$functionName($params["params"]);
        if(!$returnData) {
            $returnData = array("Result"=>1007,"ResultMessage"=>"Mobile服务调用没有返回任何报文");
        }
        $data = Zend_Json::encode ( $returnData );
        echo $data;
        return;
    }
    
    public function deleteAction() {
        return false;
    }
     /**
     * @return 获取个人国际运单详情
     */
    protected function WayInfo($params) {
        $isAuth = $this->analysisAuth($params["head"]);
        if($isAuth["Result"] != 1) {
            return $isAuth;
        }
        $waySearch = new Application_Model_Filter_WayBillSearch();
        $waySearch->userid = $isAuth["userId"];
        $pageindex = $params["idx"]?($params["idx"]==1?0:($params["idx"]-1)*10):0;
        $wayItem = Application_Model_wayBillItem::getBillAll($waySearch, 10, $pageindex);
        $returnData = array();
        if($wayItem){
            $returnData = $this->analysisWay($wayItem);
        }
        $returnData["Result"] = 0;
        $returnData["ResultMessage"] = "获取成功";
        $returnData["auth"] = Uuch_Util::encryptString($isAuth["userId"]."&".date("Y-m-d H:i:s"));
        return $returnData;
    }
    /**
     * @return 解析返回国际运单详情所需数据
     */
    protected function analysisWay($wayItem) {
        $returnData = array();
        $noAchieveNum = 0;
        $i = 0;
        foreach ($wayItem["list"] as $wayVal) {
            $returnData["wayInfo"][$i]["wayId"] = $wayVal->Id;
            $returnData["wayInfo"][$i]["billNo"] = $wayVal->BillNo;
            $returnData["wayInfo"][$i]["expressName"] = $wayVal->ExpressBy->Name;
            $returnData["wayInfo"][$i]["wayConsignee"] = $wayVal->Consignee;
            $returnData["wayInfo"][$i]["wayLogistics"] = $wayVal->Logistics; 
            $returnData["wayInfo"][$i]["wayStatus"] = $wayVal->Status;
            $returnData["wayInfo"][$i]["wayStatusName"] = $wayVal->StatusName;
            if($wayVal->Status == Application_Model_wayBillItem::STATUS_Sended) {
                $noAchieveNum++;
            }
            $i++;
        }
        $returnData["count"] = $wayItem["size"];
        $returnData["pages"] = $wayItem["pages"];
        $returnData["noAchieveNum"] = $noAchieveNum;
        return $returnData;
    }
    
    protected function AchieveWay($params) {
        $isAuth = $this->analysisAuth($params["head"]);
        if($isAuth["Result"] != 1) {
            return $isAuth;
        }
        $wayItem = Application_Model_wayBillItem::load($params["WayId"]);
        if ( !$wayItem || $wayItem->userId != $isAuth["userId"] ) {
            return array("Result"=>1002,"ResultMessage"=>"传入参数错误");
        }
        if ( ($wayItem->status == Application_Model_wayBillItem::STATUS_Sended || $wayItem->status == Application_Model_wayBillItem::STATUS_Complated) && ($wayItem->interior == Application_Model_wayBillItem::Interior_Transportation || $wayItem->interior == Application_Model_wayBillItem::Interior_Overachieve)) {
            $wayItem->ActivityConsumeExchangeScore();
            $wayItem->interior = Application_Model_wayBillItem::Interior_Achieve;
            $wayItem->status = Application_Model_wayBillItem::STATUS_Complated;
            $wayItem->save();
            $wayItem->addBillLog(Application_Model_wayBillItem::Interior_Achieve);
            return $this->WayInfo($params);
        } else {
            return array("Result"=>2,"ResultMessage"=>"运单未交运，现在无法完成！");
        }
    }
    
}